//
//  AppDelegate.m
//  YRMobDemo
//
//  Created by Yc on 2023/2/24.
//

#import "AppDelegate.h"
#import <JAGatherAdSDK/JAGatherAdSDK.h>
#import <AdSupport/ASIdentifierManager.h>//适配iOS14之前
#if defined(__IPHONE_14_0)
#import <AppTrackingTransparency/AppTrackingTransparency.h>//适配iOS14
#endif
#import "NavigationVC.h"
#import "Header.h"
#import "SplashManager.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self requestIDFA];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    NavigationVC *nav = [[NavigationVC alloc] initWithRootViewController:[NSClassFromString(@"ViewController") new]];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    [[SplashManager sharedManager] showLaunchView];
    [self initSDK];
    
    return YES;
}

- (void)initSDK {
    [JAGSDKConfig setLogEnabled:YES];
    [[JAGSDKConfig shareInstance] registerAppId:appID appKey:appKeyID completion:^(BOOL result, NSError * _Nullable error) {
        if (!result) {
            NSLog(@"初始化失败:%@",error);
            [[SplashManager sharedManager] removeLaunchView];
        } else {
            [[SplashManager sharedManager] loadSplashAd];
        }
    }];
}

- (void)requestIDFA {
    if (@available(iOS 14, *)) {
        // iOS14及以上版本需要先请求权限
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            // 获取到权限后，依然使用老方法获取idfa
            if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
                NSString *idfa = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
                NSLog(@"%@",idfa);
                
            } else {
                NSLog(@"请在设置-隐私-Tracking中允许App请求跟踪");
            }
        }];
    } else {
        // iOS14以下版本依然使用老方法
        // 判断在设置-隐私里用户是否打开了广告跟踪
        if ([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) {
            NSString *idfa = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
            NSLog(@"%@",idfa);
        } else {
            NSLog(@"请在设置-隐私-广告中打开广告跟踪功能");
        }
    }
}

// 针对iOS15不弹窗问题解决方法，根据官方文档可将权限申请放在becomeActive方法
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 用户同意协议后获取
    [self requestIDFA];
}





@end
